import React from 'react';
import {StyleSheet, ToastAndroid, View, Alert, DatePickerAndroid, TimePickerAndroid, ActivityIndicator,} from 'react-native';
import TouchButton from "../widget/btn/HighlightButton";
import HttpHelper from "../helper/HttpHelper";
import * as Color from "../constant/Color";
import {UUID} from "../util/UUID";

/** LineItem */
export default class PageAlertItem extends React.Component {

    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            //默认加载
            isShow: true
        };
    }

    componentDidMount(){
        //setState()方法重新刷新界面改变loading值
        setTimeout(() => {
                this.setState({isShow: false})
            }, 1)
    }

    render() {
        return (
            <View style={styles.container}>
                <View style={{height: 15}}/>
                <TouchButton disabled={false} title={'提示框'} onPress={()=>{this._showInfo()}}/>

                <View style={{height: 15}}/>
                <TouchButton disabled={false} title={'确认框'} onPress={()=>{this._showConfirm()}}/>

                <View style={{height: 15}}/>
                <TouchButton disabled={false} title={'日期选择'} onPress={()=>{this._selectDate()}}/>

                <View style={{height: 15}}/>
                <TouchButton disabled={false} title={'时间选择'} onPress={()=>{this._selectTime()}}/>

                <View style={{height: 15}}/>
                <View style={{flexDirection:'row', justifyContent:'space-between'}}>
                    <ActivityIndicator
                        animating={this.state.isShow}
                        size="large" />
                    <TouchButton disabled={false} title={'显示'} onPress={()=>{this.setState({
                        isShow:true,
                    })}}/>
                    <TouchButton disabled={false} title={'不显示'} onPress={()=>{this.setState({
                        isShow:false,
                    })}}/>
                </View>

                <View style={{height: 15}}/>
                <TouchButton disabled={false} title={'网络加载'} onPress={()=>{this._Load()}}/>

                <View style={{height: 15}}/>
                <TouchButton disabled={false} title={'网络加载2'} onPress={()=>{this._Load2()}}/>
                <HttpHelper ref={'http'}/>
            </View>
        );
    }

    _Load2():void{
        new HttpHelper().setURL("https://www.baidu.com/")
            .setMethod(HttpHelper.GET)
            .setNotiInfo("正在加载百度")
            .setHeadParams(new Map())
            .setBodyParams(new Map())
            .setRequestId(UUID.getOne())
            .setListener((responseCode:number, res:string) => {
                console.log("responseCode:" + responseCode + ", res:" + res)
            })
            .setTimeout(10 * 1000)
            .start(true);
    }

    _Load():void{
        if(!this.refs.http) return;
        this.refs.http.setURL("https://www.baidu.com/")
            .setMethod(HttpHelper.GET)
            .setNotiInfo("正在加载百度")
            .setHeadParams(new Map())
            .setBodyParams(new Map())
            .setRequestId(UUID.getOne())
            .setListener((responseCode:number, res:string) => {
                console.log("responseCode:" + responseCode + ", res:" + res)
            })
            .setTimeout(10 * 1000)
            .start(true);
    }

    /** 选择日期 */
    async _selectDate(): void {
        try {
            const {action, year, month, day} = await DatePickerAndroid.open({
                // 要设置默认值为今天的话，使用`new Date()`即可。月份是从0开始算的。
                date: new Date()
                // date: new Date(2018, 3, 22)
            });
            if (action !== DatePickerAndroid.dismissedAction) {
                // 这里开始可以处理用户选好的年月日三个参数：year, month (0-11), day
                this._show('year:' + year + ', month:' + (month + 1) + ', day:' + day)
            }
        } catch ({code, message}) {
            this._show('code:' + code + ', message:' + message);
        }
    }

    /** 选择时间 */
    async _selectTime(): void {
        try {
            const {action, hour, minute} = await TimePickerAndroid.open({
                // hour: 14,
                // minute: 0,
                is24Hour: true,
            });
            if (action !== TimePickerAndroid.dismissedAction) {
                // 这里开始可以处理用户选好的时分两个参数：hour (0-23), minute (0-59)
                this._show('hour:' + hour + ', minute:' + minute)
            }
        } catch ({code, message}) {
            this._show('code:' + code + ', message:' + message);
        }
    }

    /** 显示提示框 */
    _showInfo():void{
        Alert.alert(
            '提示框',
            '请知晓',
            [
                {text: 'OK',            onPress: () => this._show('OK Pressed')},
            ],
            {
                cancelable: true,
            }
        )
    }

    /** 显示确认框 */
    _showConfirm():void{
        Alert.alert(
            '确认框',
            '请选择',
            [
                {text: 'Ask me later', color:Color.orange,  onPress: () => this._show('Ask me later pressed')},
                {text: 'Cancel',        onPress: () => this._show('Cancel Pressed'), style: 'cancel'},
                {text: 'OK',            onPress: () => this._show('OK Pressed')},
            ],
            {
                cancelable: true,
                textColor:Color.orange
            }
        )
    }

    /** 显示文字 */
    _show(text:string):void{
        ToastAndroid.show (text, ToastAndroid.SHORT);
    }
}

//样式定义
const styles = StyleSheet.create({
    container: {
        flexDirection: 'column',
        alignItems: 'center',
        // justifyContent: 'center',
    },
});


